import 'package:flutter/material.dart';
import 'package:flutter_enjoy_plus/pages/404.dart';
import 'package:flutter_enjoy_plus/pages/house/house_list.dart';
import 'package:flutter_enjoy_plus/pages/house/location_list.dart';
import 'package:flutter_enjoy_plus/pages/login/index.dart';
import 'package:flutter_enjoy_plus/pages/notice/index.dart';
import 'package:flutter_enjoy_plus/pages/profile/index.dart';
import 'package:flutter_enjoy_plus/pages/tabs_page.dart';
import 'package:flutter_enjoy_plus/utils/TokenManager.dart';

void main() {
  runApp(MaterialApp(
    routes: {
      '/': (context) => const TabsPage(),
      '/noticeDetail': (context) => const NoticeDetail(),
      '/login': (context) => const LoginPage(),
      // '/profile': (context) => const ProfilePage(),
    },
    initialRoute: '/',

    // 路由拦截
    onGenerateRoute: (settings) {
      // 获取token
      String token = tokenManager.getToken();
      if (token.isEmpty) {
        return MaterialPageRoute(builder: (context) {
          return const LoginPage();
        });
      } else {
        if (settings.name == '/profile') {
          return MaterialPageRoute(builder: (context) {
            return ProfilePage(userInfo: settings.arguments as Map);
          });
        }
        if (settings.name == '/house') {
          return MaterialPageRoute(builder: (contex) {
            return const HouseList();
          });
        }
        if (settings.name == '/addHouse') {
          return MaterialPageRoute(builder: (context) {
            return const LocationList();
          });
        }
      }
      return MaterialPageRoute(builder: (context) {
        return const NotFound();
      });
    },
  ));
}
